<template>
  <div class="panel-section">
    <h3 class="section-title">
      <i class="fas fa-home"></i>相关单位
    </h3>
    <!-- 修改为垂直排列的服务项 -->
    <div class="services-grid">
      <div class="service-item" v-for="item in unitListShow" :key="item.id" @click="unitClickHandler(item)">
        <div class="service-icon">
          <img :src="item.icon" alt="logo" v-if="item.icon && item.icon != ''">
          <i class="fa-solid fa-house" v-else></i>
        </div>
        <span>{{ item.org_name }}</span>
      </div>
      <div class="service-item">
        <div class="service-icon">
          <img src="@/assets/img/logo.png" alt="logo">
        </div>
        <span>银行服务</span>
        <button class="btn-read-more" @click="openBankModal()">
          <i class="fas fa-arrow-right"></i>
        </button>
      </div>
    </div>

    <el-dialog :visible.sync="showDialog" append-to-body :custom-class="'el-dialog_unit'" width="70%">
      <div slot="title" class="dialog-title">
        <el-input placeholder="请输入搜索内容" prefix-icon="el-icon-search" v-model="searchVal" class="search-input">
        </el-input>
      </div>
      <div class="services-row">
        <div class="service-item" v-for="item in filteredData" :key="item.id" @click="unitClickHandler(item)">
          <div class="service-icon">
            <img :src="item.icon" alt="logo" v-if="item.icon && item.icon != ''">
            <i class="fa-solid fa-house" v-else></i>
          </div>
          <span>{{ item.org_name }}</span>
        </div>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { MessageBox, Message } from 'element-ui';

export default {
  name: 'UnitList',
  data() {
    return {
      unitListShow: [
        {
          id: 1,
          org_name: '中国人民银行',
          org_url: 'https://www.pbc.gov.cn/',
          icon: ''
        },
        {
          id: 2,
          org_name: '国家外汇管理局',
          org_url: 'https://www.safe.gov.cn/',
          icon: ''
        },
        {
          id: 3,
          org_name: '中国外汇交易中心',
          org_url: 'https://www.chinamoney.com.cn/',
          icon: ''
        }
      ],
      unitList: [
        {
          id: 4,
          org_name: '中国工商银行',
          org_url: 'https://www.icbc.com.cn/',
          icon: ''
        },
        {
          id: 5,
          org_name: '中国建设银行',
          org_url: 'https://www.ccb.com/',
          icon: ''
        },
        {
          id: 6,
          org_name: '中国农业银行',
          org_url: 'https://www.abchina.com/',
          icon: ''
        },
        {
          id: 7,
          org_name: '中国银行',
          org_url: 'https://www.boc.cn/',
          icon: ''
        }
      ],
      showDialog: false,
      searchVal: ''
    }
  },
  created() {
    // 这里可以调用API获取相关单位列表
    // this.getUnitList();
  },
  computed: {
    filteredData() {
      if (!this.searchVal) {
        return this.unitList;
      }
      return this.filterData(JSON.parse(JSON.stringify(this.unitList)), this.searchVal.toLowerCase());
    }
  },
  methods: {
    filterData(data, keyword) {
      return data.filter(item => {
        const match = item.org_name.toLowerCase().includes(keyword);
        return match;
      });
    },
    
    /**
     * 获取相关单位列表（占位符实现）
     */
    getUnitList() {
      // 这里应该调用实际的API
      // 目前使用静态数据
      console.log('获取相关单位列表');
    },
    
    openBankModal() {
      this.showDialog = true;
    },
    
    /**
     * 相关单位点击跳转
     * @param item 
     */
    unitClickHandler(item) {
      if (item.org_url) {
        MessageBox.confirm('确定要前往 ' + item.org_name + ' 吗？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'info'
        }).then(() => {
          window.open(item.org_url, '_blank');
        })
      } else {
        Message.error('无效链接');
      }
    }
  }
};
</script>

<style lang="less" scoped>
.panel-section {
  margin-bottom: 30px;
}

.section-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 2px solid #e6e6e6;
}

.section-title i {
  color: #2196f3;
}

.services-grid {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.service-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: white;
  border: 1px solid #e6e6e6;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.3s ease;
  position: relative;
}

.service-item:hover {
  background: #f8f9fa;
  border-color: #2196f3;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(33, 150, 243, 0.1);
}

.service-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
  border-radius: 6px;
  flex-shrink: 0;
}

.service-icon img {
  width: 24px;
  height: 24px;
  border-radius: 4px;
}

.service-icon i {
  color: #2196f3;
  font-size: 16px;
}

.service-item span {
  flex: 1;
  font-size: 14px;
  color: #333;
  font-weight: 500;
}

.btn-read-more {
  background: #2196f3;
  color: white;
  border: none;
  border-radius: 4px;
  width: 24px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-read-more:hover {
  background: #1976d2;
}

.dialog-title {
  display: flex;
  justify-content: center;
}

.search-input {
  width: 100%;
  max-width: 300px;
}

.services-row {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
}

.services-row .service-item {
  width: 30%;
  font-size: 16px;
  background-color: #fff;
}

.services-row .service-icon i {
  color: rgba(21, 101, 192, 0.85);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .services-row .service-item {
    width: 45%;
  }
}

@media (max-width: 480px) {
  .services-row .service-item {
    width: 100%;
  }
}
</style>
